<%#
kind: snippet
name: puppetlabs_repo
model: ProvisioningTemplate
snippet: true
-%>
<%
http_proxy   = host_param('http-proxy') ? " --httpproxy #{host_param('http-proxy')}" : nil
http_port    = host_param('http-proxy-port') ? " --httpport #{host_param('http-proxy-port')}" : nil
proxy_uri    = host_param('http-proxy') ? "http://#{host_param('http-proxy')}:#{host_param('http-proxy-port')}" : nil
proxy_string = proxy_uri ? " -e https_proxy=#{proxy_uri}/" : ''
proxy_string_bits = proxy_uri ? " -ProxyUsage Override -ProxyList #{proxy_uri}" : ''
os_family = @host.operatingsystem.family
os_major  = @host.operatingsystem.major.to_i
os_name   = @host.operatingsystem.name

if os_family == 'Redhat'
  repo_host = 'yum.puppet.com'
  if os_name == 'Fedora'
    repo_os = 'fedora'
  else
    repo_os = 'el'
  end
elsif os_family == 'Suse'
  repo_host = 'yum.puppet.com'
  repo_os = 'sles' # PuppetLabs repos only exist for SLES, not OpenSUSE
elsif os_family == 'Debian'
  repo_host = 'apt.puppet.com'
  repo_os = @host.operatingsystem.release_name
elsif os_family == 'Windows'
  repo_host = 'downloads.puppet.com'
  repo_os = 'windows'
end

if host_param_true?('enable-puppetlabs-repo')
  repo_name = 'puppetlabs-release'
  repo_subdir = ''
elsif host_param_true?('enable-puppetlabs-puppet6-repo')
  repo_name = 'puppet6-release'
  repo_subdir = 'puppet6/'
elsif host_param_true?('enable-puppetlabs-puppet5-repo')
  repo_name = 'puppet5-release'
  repo_subdir = 'puppet5/'
end
-%>
<% if repo_name -%>
<% if os_family == 'Redhat' || os_name == 'SLES' -%>
rpm -Uvh<%= http_proxy %><%= http_port %> https://<%= repo_host %>/<%= repo_subdir %><%= repo_name %>-<%= repo_os %>-<%= os_major %>.noarch.rpm
<% elsif os_family == 'Debian' -%>
apt-get update
apt-get -y install ca-certificates
wget -O /tmp/<%= repo_name %>-<%= repo_os %>.deb<%= proxy_string %> https://<%= repo_host %>/<%= repo_name %>-<%= repo_os %>.deb
dpkg -i /tmp/<%= repo_name %>-<%= repo_os %>.deb
<% elsif os_family == 'Windows' -%>
$puppet_agent_source = 'https://<%= repo_host %>/<%= repo_os %>/puppet-agent-<%= @host.architecture %>-latest.msi'
$puppet_agent_msi = "${env:TEMP}\puppet-agent-<%= @host.architecture %>.msi"
Write-Host "Downloading puppet-agent from ${$puppet_agent_source} to ${puppet_agent_msi}"
Start-BitsTransfer -Source "${puppet_agent_source}" -Destination "${puppet_agent_msi}"<%= proxy_string_bits %>
<% end -%>
<% end -%>
